package ogl;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;


import static org.lwjgl.opengl.GL11.*;

public class Main{


	private static Game game;

	public static void main(String[] args){

		//Initializare
		initDisplay();
		initGL();
		initGame();

		//GameEngine
		displayLoop();

		//CleanUp
		cleanUp();
	}

	//Display
	private static void initDisplay(){
		try 
		{
			Display.setDisplayMode(new DisplayMode(1024,768));
			Display.create();
			Display.setVSyncEnabled(true);/*Pentru evitarea glitchurilor provocate
										  de refreshrateul monitoarelor
			
										  */
			
			Keyboard.create();
		} 
		
		catch (LWJGLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	//InitGL
	private static void initGL(){

		//Matricea de proiectie
		glMatrixMode(GL_PROJECTION);//Folosim matricea de proiectie
		glLoadIdentity();//"Initializam matricea de proiectie
		glOrtho(0, Display.getWidth(), 0, Display.getHeight(), -1, 1);//Folosim vederea ortografica nu pe cea in perspectiva


		//Matricea model
		glMatrixMode(GL_MODELVIEW);


		//Clear-Color GL
		glClearColor(0, 1, 0, 1);//ultimul parametru = opacitatea


		glDisable(GL_DEPTH_TEST);//Sa nu tina informatiile despre ce desenam in 3D (desenam in 2D)

	}

	private static void initGame() {

		game = new Game();

	}

	private static void render(){
		glClear(GL_COLOR_BUFFER_BIT);//Stergem tot ce are frameul desenat

		glLoadIdentity();//Initializam din nou matricea sa nu avem o "animatie"


		game.render();

		Display.update();

		Display.sync(60);//Setam FPS
	}


	//Loop
	private static void displayLoop(){

		while(!Display.isCloseRequested()){

			render();
		}
	}



	//CleanUp
	private static void cleanUp(){

		Display.destroy();
		Keyboard.destroy();

	}
	
	
}